package components.scanario.elements
{
	import UIFree.ObjectEvent;
	
	import classes.c_events;
	
	import components.scanario.c_scenarioManager;
	import components.scanario.scenaries.c_baseScenario;
	
	import flash.events.Event;

	public class c_eventElement extends c_scenarioElem {
		private var _event	: String;
		public function c_eventElement(a_sm: c_baseScenario, a_event: String, a_description:String) {
			super(a_sm, a_description);
			_event 	= a_event;
		}
		
		override protected function listenEvents(): void {
			super.listenEvents();
			workspace.addEventListener(c_events.CHECKUIEVENT, doCheckUIEvents);
		}
		
		override protected function removeEvents():void {
			workspace.removeEventListener(c_events.CHECKUIEVENT, doCheckUIEvents);			
			super.removeEvents();
		}
		
		private function doCheckUIEvents(e: ObjectEvent): void {
			var obj: Object = {
				type	: e.object.type,
				result	: false
			}
			workspace.dispatchEvent(new ObjectEvent(c_events.CHECKUIEVENT, obj));
			if (obj.result) {
				checkElemScenary(e.object);
			}
		}
		
		private function checkElemScenary(obj: Object): void {
			if (!doCheck(obj)) {
				obj.result = false;
				manager.actionNoScripted();
			} else manager.nextElement();
		}
		
		override protected function doCheck(obj: Object): Boolean {
			return (obj.type && (obj.type == _event));
		}
	}
}